package net.cmkj.mine.util.tpp.coder;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;

public class WMetaBuilder extends MetaBuilder {
	
	private String createTableName;
	 
	public String getCreateTableName() {
		return createTableName;
	}
 
	public void setCreateTableName(String createTableName) {
		this.createTableName = createTableName;
	}

	public WMetaBuilder(DataSource dataSource) {
		super(dataSource);
	}

	@Override
	protected boolean isSkipTable(String tableName) {
		if(!tableName.equals(createTableName)){
			return true;
		}
		return false;
	}
	
	public List<Map<String, String>> getAllTableName(String... excludedTables) {
		List<Map<String, String>> tables = new ArrayList<Map<String, String>>();
		try {
			this.addExcludedTable(excludedTables);
			this.conn = dataSource.getConnection();
			this.dbMeta = conn.getMetaData();
			ResultSet rs = getTablesResultSet();
			while (rs.next()) {
				String tableName = rs.getString("TABLE_NAME");
				if (!this.excludedTables.contains(tableName)) {
					continue;
				}
				Map<String, String> table = new HashMap<String, String>();
				table.put("tableName", tableName);
				String className = tableName.replaceFirst("t_", "");
				className = StrKit.firstCharToUpperCase(StrKit.toCamelCase(className));
				table.put("className", className);
				tables.add(table);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tables;
	}

}
